#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: Florian Dorn <florian.dorn@gmail.com>
# @package: agenda
# @license: GNU GPLv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
from agenda.pluginsystem import *
import cgi
import re, htmlentitydefs
from agenda.appointment import *
from agenda import Emit

class CalendarProviderBase(Plugin):
	
	capabilities = [] #essentially the type of plugin
	config_defaults = {}
	
	__gsignals__ = {
		#'updating': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,[gobject.TYPE_FLOAT]),
	    'event-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,[gobject.TYPE_PYOBJECT]),
	    'calendar-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,[gobject.TYPE_PYOBJECT]),
	}
	"""This is the base class for all calendar Providers"""
	def __init__(self):
		Plugin.__init__(self)
		self.errors = []
		self.timezoneadjust = 0
		self.disabled = False
		#self.eventStorage = []
		#self.calendarStorage = {}
		self.update_ok = False
		self.active = False
		self._has_config_widget = False
		
	def is_active(self):
		return self.is_active
	def initPlugin(self,options):
		self.setOptions(options)
		pass

	def clearErrors(self):
		self.errors = []
	def notifyError(self,errorMessage):
		"""Notify the user on Errors that occured during the update"""
		print errorMessage
		self.errors.append(errorMessage)
	def getErrors(self):
		"""return the errors"""
		return self.errors
	def deleteEvents(self):
		pass
		#self.eventStorage = []
		#self.calendarStorage = {}
	def add_event(self,event):
		Emit(self,'event-added',event)
		
	def create_event(self,what="new Event",start_time=None,end_time=None,where="",description="",recurrence_data=None):
		"""
		create an event.
		start_time and end_time are datetime objects without TZinfo, which is interpreted as GMT
		recurrence data follows the iCalendar standard (RFC 2445 http://www.ietf.org/rfc/rfc2445.txt)
		@see appointment.add_event for more details
		"""
		pass

	def add_calendar(self,calendar):
		
		Emit(self,'calendar-added',calendar)
		#self.calendarStorage[calendar.id] = calendar

	def ensureString(self,val):
		if (val == None):
			return ""
		else:
			return val
	def unescape(self,text):
		def fixup(m):
			text = m.group(0)
			if text[:2] == "&#":
				# character reference
				try:
					if text[:3] == "&#x":
						return unichr(int(text[3:-1], 16))
					else:
						return unichr(int(text[2:-1]))
				except ValueError:
					pass
			else:
				# named entity
				try:
					text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
				except KeyError:
					pass
			return text # leave as is
		return re.sub("&#?\w+;", fixup, text)
	def escape(self,s):
		return cgi.escape(s)

	def list_calendars(self):
		""" return a list with all calendars available with this provider"""
		return []
	def config_change_cb(self,appointments,path,value):
		print self
		print "%s: %s" % (path,value)
		pass
	def read_events(self):
		return False
	def read_tasks(self):
		print "read_tasks"
		return False
	
	def has_config_widget(self):
		"""
		returns true if the provider has a configuration dialog
		"""
		return self._has_config_widget
	def get_config_widget(self):
		"""
		return the configuration widget
		"""
		return None

class CalendarBackenReadError(BaseException):
	def __init__(self):
		pass
		
